﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Petshop.Backend.Data
{
    class AccountRepository : Petshop.Backend.Data.IAccountRepository, IDisposable
    {
        private readonly IDatabaseSession _databaseSession;

        public AccountRepository(IDatabaseSession databaseSession)
        {
            _databaseSession = databaseSession;
        }
        public Account FindBy(string userId)
        {
            var account = _databaseSession.Context.Accounts.SingleOrDefault(a => a.UserId == userId);
            return account;
        }
        public Account FindBy(string userId, string password)
        {
            var account = _databaseSession.Context.Accounts.SingleOrDefault(a => a.UserId == userId & a.Password == password);
            return account;
        }
        public void Insert(Account account)
        {
            _databaseSession.Context.AddToAccounts(account);
        }
        public void Update(Account account)
        {
            _databaseSession.Context.ObjectStateManager.ChangeObjectState(account, EntityState.Modified); 
        }
        public void Save()
        {
            _databaseSession.Context.SaveChanges();
        }
        #region IDisposable
        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _databaseSession.Context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        } 
        #endregion





       
    }
}
